package com.bella.api.utils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SignHelper {
    private static String sign(String content)
    {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            byte[] encodedhash = digest.digest(content.getBytes(StandardCharsets.UTF_8));

            return bytesToHex(encodedhash);
        }
        catch (NoSuchAlgorithmException  e) {
            return null;
        }
    }
    public static boolean validateSign(String sign,String content){
        String signGain = sign(content);
        return signGain.equals(sign);
    }
    private static String bytesToHex(byte[] hash)
    {
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < hash.length; i++) {
            String hex = Integer.toHexString(0xff & hash[i]);
            if(hex.length() == 1) hexString.append('0');
                hexString.append(hex);
        }
        return hexString.toString();
    }
}
